package work.jame.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import work.jame.blog.framework.exception.BusinessException;
import work.jame.blog.framework.exception.db.DataNotExistException;
import work.jame.blog.framework.exception.db.DeleteDataBaseException;
import work.jame.blog.framework.exception.db.InsertDataBaseException;
import work.jame.blog.framework.utils.security.SpringSecurityUtils;
import work.jame.blog.pojo.UserArticleCollect;
import work.jame.blog.pojo.UserCollectGroup;
import work.jame.blog.mapper.UserCollectGroupMapper;
import work.jame.blog.pojo.vo.ArticleCollectGroupVO;
import work.jame.blog.service.UserArticleCollectService;
import work.jame.blog.service.UserCollectGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jame
 * @since 2023-05-18
 */
@Service
public class UserCollectGroupServiceImpl extends ServiceImpl<UserCollectGroupMapper, UserCollectGroup> implements UserCollectGroupService {

    @Autowired
    private UserArticleCollectService userArticleCollectService;

    @Resource
    private UserCollectGroupMapper userCollectGroupMapper;

    @Override
    public boolean checkGroupNameExist(String groupName) {
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        QueryWrapper<UserCollectGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("group_name", groupName);
        return this.count(wrapper) > 0;
    }

    @Override
    public boolean saveGroup(UserCollectGroup userCollectGroup) {
        if (this.checkGroupNameExist(userCollectGroup.getGroupName())) {
            throw new BusinessException("已存在相同组名");
        }
        userCollectGroup.setUserId(SpringSecurityUtils.getCurrentLoginUserId());
        return this.save(userCollectGroup);
    }

    @Override
    public boolean updateGroupById(UserCollectGroup userCollectGroup) {
        if (this.checkGroupNameExist(userCollectGroup.getGroupName())) {
            throw new BusinessException("已存在相同组名");
        }
        userCollectGroup.setUserId(SpringSecurityUtils.getCurrentLoginUserId());
        return this.updateById(userCollectGroup);
    }

    @Override
    @Transactional
    public boolean deleteCollectGroup(Long groupId) {
        //不检查删除的组是否是默认组,允许删除默认组
        UserCollectGroup group = this.getById(groupId);
        if (Objects.isNull(group)) {
            throw new DataNotExistException("组不存在");
        }
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        if (!group.getUserId().equals(userId)) {
            throw new BusinessException("不能删除不是自己的组");
        }
        if (!userArticleCollectService.deleteByGroupId(groupId)) {
            throw new DeleteDataBaseException("删除组内收藏失败");
        }
        return this.removeById(groupId);
    }

    @Override
    public boolean queryGroupCreateUser(Long groupId, Long userId) {
        QueryWrapper<UserCollectGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId)
                .eq("user_id", userId);
        return this.count(wrapper) == 1;
    }

    @Override
    public void saveUserDefaultGroup(Long userId) {
        //给用户添加一个默认收藏分组
        UserCollectGroup group = new UserCollectGroup();
        group.setUserId(userId);
        group.setGroupName("默认分组");
        group.setGroupIsOvert(true);
        group.setSerialNumber(0);
        if (!this.save(group)) {
            throw new InsertDataBaseException("添加用户默认分组失败");
        }
    }

    @Override
    public List<ArticleCollectGroupVO> getCurrentUserCollectGroups(Long articleId) {
        Long userId = SpringSecurityUtils.getCurrentLoginUserId();
        return userCollectGroupMapper.getCurrentUserCollectGroups(articleId, userId);
    }
}
